<template>
    <div class="screen-container" :style="containerStyle">
        <div class="screen-header">
            <div>
                <img :src="headerBorderSrc" alt="">
            </div>
            <span class="logo">
                <a href="javascript:void(0);" :style='logoStyle'>电商平台logo</a>
            </span>
            <span class="title">电商平台实时监控系统</span>
            <div class="title-right">
                <img :src="themeSrc" alt="" class="qiehuan" @click="handleChangeTheme">
                <span class="datetime" ref='date'></span>
            </div>
        </div>
        <div class="screen-body">
            <section class="screen-left">
                <div id="left-top" :class="[fullScreenStatus.trend?'fullScreen':'']">
                  <Trend ref='trend'/>
                  <div class="resize">
                    <span  @click="changeSize('trend')" :class="['iconfont', fullScreenStatus.trend?'icon-compress-alt':'icon-expand-alt']"></span>
                  </div>
                </div>
                <div id="left-bottom" :class="[fullScreenStatus.seller?'fullScreen':'']">
                  <Seller ref='seller'/>
                  <div class="resize">
                    <span @click="changeSize('seller')" :class="['iconfont', fullScreenStatus.seller?'icon-compress-alt':'icon-expand-alt']"></span>
                  </div>
                </div>
            </section>
            <section class="screen-middle">
                <div id="middle-top" :class="[fullScreenStatus.map?'fullScreen':'']">
                  <Map ref='map'/>
                  <div class="resize">
                    <span @click="changeSize('map')" :class="['iconfont', fullScreenStatus.map?'icon-compress-alt':'icon-expand-alt']"></span>
                  </div>
                </div>
                <div id="middle-bottom" :class="[fullScreenStatus.rank?'fullScreen':'']">
                  <Rank ref='rank'/>
                  <div class="resize">
                    <span @click="changeSize('rank')" :class="['iconfont', fullScreenStatus.rank?'icon-compress-alt':'icon-expand-alt']"></span>
                  </div>
                </div>
            </section>
            <section class="screen-right">
                <div id="right-top" :class="[fullScreenStatus.hot?'fullScreen':'']">
                  <Hot ref='hot'/>
                  <div class="resize">
                    <span @click="changeSize('hot')" :class="['iconfont', fullScreenStatus.hot?'icon-compress-alt':'icon-expand-alt']"></span>
                  </div>
                </div>
                <div id="right-bottom" :class="[fullScreenStatus.stock?'fullScreen':'']">
                  <Stock ref='stock'/>
                  <div class="resize">
                    <span @click="changeSize('stock')" :class="['iconfont', fullScreenStatus.stock?'icon-compress-alt':'icon-expand-alt']"></span>
                  </div>
                </div>
            </section>
        </div>
    </div>
</template>

<script>
    import Hot from 'components/Hot'
    import Map from 'components/Map'
    import Rank from 'components/Rank'
    import Seller from 'components/Seller'
    import Stock from 'components/Stock'
    import Trend from 'components/Trend'

    import {getThemeValue} from '@/utils/theme_utils'
    import {formatDate} from '@/utils/date_utils'
    import {mapState} from 'vuex'
    export default {
        name: '',
        data () {
            return {
              // 全屏状态
              fullScreenStatus:{
                hot:false,
                map:false,
                rank:false,
                seller:false,
                stock:false,
                trend:false
              }
            }
        },
        components: {
          Hot,
          Map,
          Rank,
          Seller,
          Stock,
          Trend
        },
        created () {
          // 注册接收数据的回调函数
          this.$socket.registerCallBack('fullScreen',this.recvData)
          this.$socket.registerCallBack('changeTheme',this.recvChangeTheme)
        },
        mounted () {
          // 展示时间
          this.getDate()
        },
        destroyed () {
          this.$socket.unRegisterCallBack('fullScreen')
          this.$socket.unRegisterCallBack('changeTheme')
        },
        computed: {
          ...mapState(['theme']),
          containerStyle(){
            return {
              backgroundColor:getThemeValue(this.theme).backgroundColor,
              color:getThemeValue(this.theme).titleColor
            }
          },
          logoStyle(){
            return {
              color:getThemeValue(this.theme).titleColor
            }
          },
          headerBorderSrc(){
            return "/static/img/"+getThemeValue(this.theme).headerBorderSrc
          },
          themeSrc(){
            return "/static/img/"+getThemeValue(this.theme).themeSrc
          }
        },
        methods: {
          changeSize(chartName){
            // 1.改变fullScreenStatus中的数据
            // this.fullScreenStatus[chartName]=!this.fullScreenStatus[chartName]
            // 2.需要调用每个图表的screenAdapter方法
            // 数据更新后需要延时一段时间才能更新图表
            // 这个时间是数据更新后下一次组件更新完成的时机
            // this.$nextTick(()=>this.$refs[chartName].screenAdapter())

            // 联动效果改造
            // 将数据发送给服务器
            const targetValue=!this.fullScreenStatus[chartName]
            this.$socket.send({
              action:'fullScreen',
              socketType:'fullScreen',
              chartName:chartName,
              value:targetValue
            })
          },
          // 接收到服务器发送的全屏数据处理
          recvData(data){
            // 取出是哪一个图表需要切换
            const chartName=data.chartName
            // 取出图表状态
            const value=data.value
            this.fullScreenStatus[chartName]=value
            this.$nextTick(()=>this.$refs[chartName].screenAdapter())
          },
          handleChangeTheme(){
            // 修改vuex中的数据
            // this.$store.commit('changeTheme')
            // 联动效果改造
            this.$socket.send({
              action:'changeTheme',
              socketType:'changeTheme',
              chartName:'',
              value:''
            })
          },
          recvChangeTheme(){
            this.$store.commit('changeTheme')
          },
          getDate(){
            setInterval(() => {
              let date=new Date
              this.$refs.date.innerHTML=formatDate(date,'yyyy-MM-dd hh:mm:ss')
            }, 1000);
          }
        }
    }
</script>

<style scoped lang="less">
// 全屏样式的定义
.fullScreen {
  position: fixed!important;
  top: 0 !important;
  left: 0 !important;
  width: 100% !important;
  height: 100% !important;
  margin: 0 !important;
  z-index: 100;
}

.screen-container {
  width: 100%;
  height: 100%;
  padding: 0 20px;
  background-color: #161522;
  color: #fff;
  box-sizing: border-box;
  user-select: none;
}
.screen-header {
  width: 100%;
  height: 64px;
  font-size: 20px;
  position: relative;
  > div {
      position: absolute;
      top: 0;
      img {
        width: 100%;
      }
    }
  .title {
    position: absolute;
    left: 50%;
    top: 50%;
    font-size: 20px;
    transform: translate(-50%, -50%);
  }
  .title-right {
    display: flex;
    align-items: center;
    position:absolute;
    right: 0px;
    top: 50%;
    transform: translateY(-80%);
  }
  .qiehuan {
    width: 28px;
    height: 21px;
    cursor: pointer;
  }
  .datetime {
    font-size: 15px;
    margin-left: 10px;
  }
  .logo {
    position: absolute;
    left: 0px;
    top: 50%;
    transform: translateY(-80%);
    a{
      text-decoration: none;
    }
  }
}
.screen-body {
  width: 100%;
  height: 100%;
  display: flex;
  margin-top: 10px;
  .screen-left {
    height: 100%;
    width: 27.6%;
    #left-top {
      height: 53%;
      position: relative;
    }
    #left-bottom {
      height: 31%;
      margin-top: 25px;
      position: relative;
    }
  }
  .screen-middle {
    height: 100%;
    width: 41.5%;
    margin-left: 1.6%;
    margin-right: 1.6%;
    #middle-top {
      width: 100%;
      height: 56%;
      position: relative;
    }
    #middle-bottom {
      margin-top: 25px;
      width: 100%;
      height: 28%;
      position: relative;
    }
  }
  .screen-right {
    height: 100%;
    width: 27.6%;
    #right-top {
      height: 46%;
      position: relative;
    }
    #right-bottom {
      height: 38%;
      margin-top: 25px;
      position: relative;
    }
  }
}
.resize{
  position:absolute;
  right: 20px;
  top: 20px;
  cursor: pointer;
}
</style>
